#!/bin/bash

echo "🚀 启动Ubuntu桌面环境 + noVNC..."
echo

# 检查Docker
if ! command -v docker &> /dev/null; then
    echo "❌ Docker未安装"
    exit 1
fi

if ! docker info &> /dev/null; then
    echo "❌ Docker服务未运行"
    exit 1
fi

# 停止现有容器
echo "1. 停止现有容器..."
docker-compose -f docker-compose.lightweight.yml down 2>/dev/null || true
docker-compose down 2>/dev/null || true

# 构建新镜像
echo "2. 构建带VNC的镜像..."
docker build -f Dockerfile.lightweight-vnc -t ubuntu-desktop:lightweight-vnc .

if [ $? -ne 0 ]; then
    echo "❌ 镜像构建失败"
    exit 1
fi

# 启动noVNC服务
echo "3. 启动noVNC服务..."
docker-compose -f docker-compose-novnc.yml up -d

if [ $? -ne 0 ]; then
    echo "❌ 服务启动失败"
    exit 1
fi

# 等待服务启动
echo "4. 等待服务启动..."
sleep 10

# 检查服务状态
echo "5. 检查服务状态..."
docker-compose -f docker-compose-novnc.yml ps

echo
echo "=================================="
echo "🎉 Ubuntu桌面环境 + noVNC启动成功！"
echo "=================================="
echo
echo "访问方式："
echo "  🌐 Web界面: http://localhost:8080"
echo "  📱 VNC客户端: localhost:5902"
echo "  💻 SSH: ssh -p 2224 ubuntu@localhost"
echo
echo "推荐使用Web界面访问："
echo "  1. 打开浏览器访问: http://localhost:8080"
echo "  2. 点击 'Connect' 按钮"
echo "  3. 无需密码即可进入桌面"
echo
echo "管理命令："
echo "  查看状态: docker-compose -f docker-compose-novnc.yml ps"
echo "  查看日志: docker-compose -f docker-compose-novnc.yml logs -f"
echo "  停止服务: docker-compose -f docker-compose-novnc.yml down"
echo 